const QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
let qqmapsdk,
  App = getApp()
Page({

  /**
   * 页面的初始数据
   */
  midData: {
    address: '',
    location: {
      lat: '',
      lng: ''
    }

  },
  data: {
    longitude: '116.304072',
    latitude: '39.974198',
    selectedLong: '',
    selectedLati: '',
    detailAddress: '',
    markers: [{
      longitude: '116.304072',
      latitude: '39.974198',
      iconPath: '../../static/images/circle.png',
      width: 40,
      height: 40,
      anchor: {
        x: .5,
        y: .5
      }
    }],
    keyword: '',
    addressList: [],
    pickerShow: false,
    pickerValue: ''
  },
  onLoad(options) {
    // 实例化API核心类
    qqmapsdk = new QQMapWX({
      key: 'AFDBZ-5EDCX-36E43-TZQNP-RNCXQ-M6FBT'
    });
    this.getLocation()
  },
  onReady() {

  },
  onShow() {

  },
  getLocation() {
    wx.getLocation({
      type: 'gcj02',
      success: res => {
        console.log(res)
        this.setData({
          latitude: res.latitude,
          longitude: res.longitude,
          'markers[0].latitude': res.latitude,
          'markers[0].longitude': res.longitude
        })
        this.reverseGeocoder(res.longitude, res.latitude)
      }
    })
  },
  // 地图拖动；
  regionChange(e) {
    if (e.type !== 'end') return;
    const {
      longitude,
      latitude
    } = e.detail.centerLocation;
    this.reverseGeocoder(longitude, latitude)
  },
  // 逆地理解析
  reverseGeocoder(longitude, latitude) {
    let _this = this;
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude
      },
      success: res => {
        _this.setData({
          detailAddress: res.result.address + ' ' + res.result.formatted_addresses.recommend,
          selectedLati: latitude,
          selectedLong: longitude
        })
      }
    })
  },
  poiTap(e) {
    this.setData(e.detail)
  },
  searchConfirm(e) {
    const value = e.detail.value;
    this.searchMap(value)
  },
  inputHandler(e) {
    const value = e.detail.value;
    this.searchMap(value)
  },
  searchMap(value) {
    let _this = this;
    qqmapsdk.search({
      keyword: value,
      location: {
        latitude: _this.data.latitude,
        longitude: _this.data.longitude
      },
      success: res => {
        console.log(res)
        res.data[0] && (_this.midData.address = res.data[0].address);
        _this.setData({
          addressList: res.data,
          pickerShow: true
        })
      }
    })
  },
  pickerChange(e) {
    console.log(e);
    let index = e.detail.value[0];
    this.midData.address = this.data.addressList[index].address;
    this.midData.location = this.data.addressList[index].location;
  },
  pickerCancel() {
    this.setData({
      pickerShow: false
    })
  },
  pickerConfirm() {
    
    this.setData({
      pickerShow: false,
      detailAddress: this.midData.address,
      keyword: this.midData.address,
      latitude: this.midData.location.lat,
      longitude: this.midData.location.lng
    })
  },
  confirm() {
    App.globalData.location = this.data.detailAddress;
    App.globalData.selectedLati = this.data.selectedLati;
    App.globalData.selectedLong = this.data.selectedLong;
    wx.navigateBack()
  }
})